package com.sogou.ime.ui.settings.dynamic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class GlobalUtils {
	
	public static int getIntValue(String num, int defaultValue)
	{
		int intValue = 0;
		try {
			intValue = Integer.parseInt(num);
		} catch (Exception e) {
			return defaultValue;
		}
		
		return intValue;
	}
	
	public static double getDoubleValue(String num, double defaultValue)
	{
		double doubleValue = 0.0;
		
		try {
			doubleValue = Double.parseDouble(num);
		} catch (Exception e) {
			return defaultValue;
		}
		
		return doubleValue;
	}
	
	public static String getSystemLanguage()
	{
		String language = Locale.getDefault().getLanguage();
		
		return language;
	}
	
	public static String SubString(String input, String start_str,
			String end_str) {
		int start_pos = input.indexOf(start_str);
		if (start_str.equals("")) {
			start_pos = 0;
		}
		if (start_pos == -1)
			return "";

		String output = null;
		int end_pos = input.indexOf(end_str, start_pos + 1);
		if (end_str.equals("")) {
			end_pos = -1;
		}

		if (end_pos == -1) {
			output = input.substring(start_pos + start_str.length());
		} else {
			output = input.substring(start_pos + start_str.length(), end_pos);
		}

		return output;
	}
	
	public static boolean showDialogMessageBox(Activity curActivity, String msgTitle, String msgContent,
			String sureString, String cancelString, 
			DialogInterface.OnClickListener sureOper,
			DialogInterface.OnClickListener cancelOper
			)
	{

		new AlertDialog.Builder(curActivity).setTitle(msgTitle).setMessage(msgContent)
			.setPositiveButton
			(
					sureString,
					sureOper	
			)
			.setNegativeButton
			(
					cancelString,     
					cancelOper
			).show();
		
		return true;
	}
	
	 public static void showToast(Handler handler, final Context context, final String toastMessage) {
		  handler.post(new Runnable() {
		   @Override
		   public void run() {
			   Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
					   }
		  });
	}
	 
	 public static String getSdCardPath()
	 {
		 return Environment.getExternalStorageDirectory().getPath();
	 }
	 
	public static String readSDFile(String fileName) {
		String SDPATH = getSdCardPath();
		StringBuffer sb = new StringBuffer();
		File file = new File(SDPATH + "//" + fileName);
		try {
			FileInputStream fis = new FileInputStream(file);
			int c;
			while ((c = fis.read()) != -1) {
				sb.append((char) c);
			}
			fis.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		return sb.toString();
	}

	 public static InputStream getInputStreamFromSdCardFile(String fileName)
 {
		InputStream in = null;
		File file = new File(fileName);
		try {
			FileInputStream fis = new FileInputStream(file);
			in = fis;
		} catch (Exception e) {
			in = null;
			Log.e("error", "getInputStreamFromSdCardFile e="+e.toString());
		}
		
		return in;
	}
	 
	 public static boolean writeFileToSdCard(Context context, String fileName, String content)
	 {
		 try{ 
			 FileOutputStream fout = new FileOutputStream(fileName, false);
			 byte [] bytes = content.getBytes(); 
			 fout.write(bytes); 
			 fout.close(); 
		 } catch(Exception e){
			 Log.d("debug", "writeFileToSdCard e="+e.toString());
			 return false;
		 }
		 
		 return true;
	 }
	 
	 public static String createXmlString(String tagName, String value, int tabIndex)
	 {
		 String tabString = "    ";		//4 space
		 StringBuilder sb = new StringBuilder();
		 for (int i=0; i<tabIndex; i++)
		 {
			 sb.append(tabString);
		 }
		 sb.append("<"+tagName+">");
		 sb.append(value);
		 sb.append("</"+tagName+">");
		 sb.append("\n");
		 
		 return sb.toString();
	 }
	 
	 public static String getTabString(int tabCount)
	 {
		 String tabString = "    ";		//4 space
		 StringBuilder sb = new StringBuilder();
		 for (int i=0; i < tabCount; i++)
		 {
			 sb.append(tabString);
		 }
		 
		 return sb.toString();
	 }
	 
	 public static String createXmlCommentString(String comment)
	 {
		 return "<!-- "+comment+" -->\n";
	 }
	 
	 public static String htmlencode(String input)
	 {
		 return input.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
			        .replace("'", "&apos;").replaceAll("\"", "&quot;");
	 }
	 
}
